package com.itdianzi.controller.login;


import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.system.Module;
import com.itdianzi.domain.system.User;
import com.itdianzi.service.system.ModuleService;
import com.itdianzi.service.system.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class LoginController extends BaseController{
    @Autowired
    private UserService userService;
    @Autowired
    private ModuleService moduleService;

	/*@RequestMapping("/login")
	public String login(String email,String password) {
        if (StringUtils.isBlank(email) || StringUtils.isBlank(password)) {

            return "forward:/login.jsp";
        }
        //检验数据库是否含有该用户的数据
        User user = userService.findByEmail(email);
        if (user == null) {
            request.setAttribute("error","用户名或密码错误");
            return "forward:/login.jsp";
        } else if (user != null && !user.getPassword().equals(password)) {
            request.setAttribute("error","用户名或密码错误");
            return "forward:/login.jsp";
        } else if (user!=null && user.getPassword().equals(password)) {
            session.setAttribute("loginUser",user);

            //处理登陆数据，用户可以查看的模块信息
            List<Module> moduleList = moduleService.findByUser(user);

            session.setAttribute("modules", moduleList);
            return "home/main";

        }
        return null;
	}
*/

    @RequestMapping("/login")
    public String login(String email,String password) {

        try {
            //1.获取用户名密码
            //2.拿到subject对象
            Subject subject = SecurityUtils.getSubject();
            //3.使用subject进行登录
            //需要构造upToken传入
            UsernamePasswordToken upToken = new UsernamePasswordToken(email, password);
            //login自动调用认证方法，如果认证成功，自动调用密码比较器方法
            subject.login(upToken);//认证成功，操作继续，认证不成功，底层直接抛出异常

            //4.手动获得shiro中的数据
            User user = (User) subject.getPrincipal();//安全数据
            //5.将数据放入seesion中
            session.setAttribute("loginUser", user);
            //6.操作模块数据（左侧菜单栏）
            List<Module> moduleList = moduleService.findByUser(user);
            session.setAttribute("modules", moduleList);
            //跳转主页
            return "home/main";
        } catch (AuthenticationException e) {
            //登录失败
            request.setAttribute("error","用户名或密码错误");
            return "forward:/login.jsp";
        }

    }

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        //SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
